###############################################
# Replication Code for                        #
# "Why Process Matters for Causal Inference"  #
# by Adam N. Glynn and Kevin M. Quinn         #
############################################### 

load("CPSvote2004.RData")

# remove North Dakota (no registration)
VOTECPSnd <- subset(VOTECPS, GESTFIPS != 38) # remove North Dakota

EDR <- rep(0,dim(VOTECPSnd)[1])
EDR[VOTECPSnd$GESTFIPS == 16] <- 1 # Idaho
EDR[VOTECPSnd$GESTFIPS == 27 ] <- 1 #Maine
EDR[VOTECPSnd$GESTFIPS == 26 ] <- 1 # Minnesota
EDR[VOTECPSnd$GESTFIPS == 33] <- 1 # New Hampshire
EDR[VOTECPSnd$GESTFIPS == 55] <- 1 # Wisconsin
EDR[VOTECPSnd$GESTFIPS == 56] <- 1 # Wyoming

VOTECPSnd$EDR <- EDR

# Remove Don't Know, Refused, No Response for turnout
VOTECPSnd <- subset(VOTECPSnd,VOTE == 1 | VOTE == 2) 
# Change "No" from 2 to 0
VOTECPSnd$VOTE[VOTECPSnd$VOTE==2] <- 0 

# Remove Don't Know, Refused, No Response for registered
VOTECPSnd <- subset(VOTECPSnd,REG == 1 | REG == 2)
# Change "No" from 2 to 0
VOTECPSnd$REG[VOTECPSnd$REG==2] <- 0

# Subset to Black Only
VOTECPSndB <- subset(VOTECPSnd,PTDTRACE == 2)


# Replicate Table 1
table(VOTECPSndB$EDR,VOTECPSndB$VOTE)

# Replicate Table 4 
table(VOTECPSndB$REG[VOTECPSndB$EDR==0], VOTECPSndB$VOTE[VOTECPSndB$EDR==0])

# Replicate Table 5
table(VOTECPSndB$REGREAS[VOTECPSndB$EDR==0 & VOTECPSndB$REG==0 ])

# Replicate Figure 2

n <- (5170+1276+676) # number of EDR = 0
p00 <- 1276/n 
p01 <- 0
p10 <- 676/n
p11 <- 5170/n

p00U0 <- 691/n # overall proportion in rows 4-12 of Table 5
p00U1 <- 585/n # overall proportion in rows 1-3 of Table 5

pRegU0 <- seq(0,1,.05) # varying effect of EDR on the EDR=0, REG=0, VOTE=0, U=0
UB <- (pRegU0*p00U0 + .05*p00U1 + .05*p10) # Estimated Upper Bound
# Calculating the Standard Error for the Upper Bound 
SE <- sqrt( pRegU0^2*(p00U0*(1-p00U0))/n + .05^2*(p00U1*(1-p00U1))/n + .05^2*(p10*(1-p10))/n 
- pRegU0*.05*p00U0*p00U1/n - pRegU0*.05*p00U0*p10/n - .05*.05*p00U1*p10/n  )  

pdf("Comparison.pdf")
plot(pRegU0,UB,type="l",ylim=c(0,.15),ylab="Effect of EDR on African American Turnout in non-EDR States",xlab="Proportion of U=0, X=0, M=0, Y=0 Individuals Caused to Register by EDR",main="Comparison of Evidence for the Effect of EDR on Turnout")
lines(pRegU0,UB+2*SE,lty=2)
lines(pRegU0,UB-2*SE,lty=2)
text(x=.55,y=.01 ,"Upper Bound of Possible Effects Under Assumptions 1,2, and 3")
arrows(x0=.55,y0=.015,x1=.65,y1=.07)
abline(h=.143)
adj <- .003
text(x=.085,y=.143 + adj,"Matching")
abline(h = .133)
abline(h = .132)
abline(h = .131)
abline(h = .129)
abline(h = .127)
text(x=.1,y=.133 + adj,"Models 1-7")
abline(h = .097)
abline(h = .096)
text(x=.1,y=.097 + adj,"Models 8-9")
dev.off()

